﻿
Partial Class WebForm_ManageStaff_SearchStaff
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.Page.IsPostBack Then
            Me.BindGV()
            Me.BindOrganization()
        End If
    End Sub

    Protected Sub lknAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lknAdd.Click
        RaiseEvent Add(Me, New EventArgs)
    End Sub

    
    Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

        Me.EmployeeID = e.CommandArgument
        If e.CommandName = "Editing" Then
            RaiseEvent Editing(Me, New EventArgs)
        ElseIf e.CommandName = "Deleting" Then
            RaiseEvent Deleting(Me, New EventArgs)
        End If
    End Sub

#Region "資料繫結"
    Protected Sub BindOrganization()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("Select OrganizationID,OrganizationName from Organization ")
        sql.AppendFormat("Where Active='Y' ")

        ddlOrganization.DataTextField = "OrganizationName"
        ddlOrganization.DataValueField = "OrganizationName"
        ddlOrganization.DataSource = dat.GetData(sql)
        ddlOrganization.DataBind()
        ddlOrganization.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Public Sub BindGV()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select e.*, o.OrganizationName ")
        sql.AppendFormat(" From Employee e Left Join Organization o ON e.OrganizationID=o.OrganizationID ")
        sql.AppendFormat(" Where 1=1 ")

        If Me.txtEmployeeID.Text <> "" Then
            sql.AppendFormat(" And e.EmployeeID = '{0}' ", Me.txtEmployeeID.Text)
        End If

        If Me.txtEmployeeName.Text <> "" Then
            sql.AppendFormat(" And e.EmployeeName Like '%{0}%' ", Me.txtEmployeeName.Text)
        End If

        If Me.txtTitle.Text <> "" Then
            sql.AppendFormat(" And e.Title Like '%{0}%' ", Me.txtTitle.Text)
        End If

        If Me.ddlOrganization.SelectedValue <> "" Then
            sql.AppendFormat(" And o.OrganizationName= '{0}' ", Me.ddlOrganization.SelectedValue)
        End If

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()

    End Sub
    Public Sub DeleteData(ByVal EmployeeID As String)
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Using scope As New Transactions.TransactionScope

            sql.Length = 0
            sql.AppendFormat("delete Employee ")
            sql.AppendFormat("where EmployeeID = '{0}' ", EmployeeID)
            dat.AccData(sql)

            scope.Complete()
        End Using
    End Sub

#End Region

#Region "Event"
    Public Event Add(ByVal source As Object, ByVal e As EventArgs)
    Public Event Editing(ByVal source As Object, ByVal e As EventArgs)
    Public Event Deleting(ByVal source As Object, ByVal e As EventArgs)
#End Region

#Region "Property"
    Public Property EmployeeID() As String
        Get
            Return ViewState("EmployeeID")
        End Get
        Set(ByVal value As String)
            ViewState("EmployeeID") = value
        End Set
    End Property
#End Region
    
End Class
